package cn.hxzy.office.controller;

import cn.hxzy.office.entity.News;
import cn.hxzy.office.entity.Project;
import cn.hxzy.office.entity.User;
import cn.hxzy.office.entity.UserSchedule;
import cn.hxzy.office.service.NewsService;
import cn.hxzy.office.service.UserScheduleService;
import cn.hxzy.office.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author little black
 * 这个是用户管理表页面
 */
@Controller
public class UserIndexController {
    @Autowired
    private NewsService newsService;
    @Autowired
    private UserScheduleService userScheduleService;
    @Autowired
    private UserService userService;

    /**
     * 日程管理
     *
     * @return
     */
    @GetMapping("/createUserSchedule")
//    新增员工日程
    public ModelAndView createUserSchedule() {
        ModelAndView modelAndView = new ModelAndView("createUserSchedule");
        return modelAndView;
    }

    @PostMapping("/saveUserSchedule")
//    员工日程->重定向
    public String saveUserSchedule(UserSchedule userSchedule,HttpSession session) {
        User user =(User) session.getAttribute("loginUser");
        userSchedule.setUser(user);
        userScheduleService.insert(userSchedule);
        return "redirect:/searchUserSchedule";
    }

    @GetMapping("/deleteUserSchedule/{id}")
//    删除日程
    public String deleteUserSchedule(@PathVariable("id") Integer id) {
        userScheduleService.deleteById(id);
        return "redirect:/searchUserSchedule";
    }

    @GetMapping("/updateUserSchedule/{id}")
//编辑日程
    public ModelAndView updateUserSchedule(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("updateUserSchedule");
        System.out.println(id);
        UserSchedule userSchedule = userScheduleService.queryById(id);
        System.out.println(userSchedule);
        modelAndView.addObject("userSchedule", userSchedule);
        return modelAndView;
    }

    @PostMapping("/editUserSchedule/{id}")
//    日程修改->重定向
    public String editUserSchedule(UserSchedule userSchedule,HttpSession session) {
        User user =(User) session.getAttribute("loginUser");
        userSchedule.setUser(user);
        userScheduleService.update(userSchedule);
        return "redirect:/searchUserSchedule";
    }

    @GetMapping("/searchUserSchedule")
//    员工日程查询
    public ModelAndView searchUserSchedule() {
        ModelAndView modelAndView = new ModelAndView("searchUserSchedule");
        List<User> users = userScheduleService.queryAll();
        modelAndView.addObject("userSchedules", users);
        return modelAndView;
    }


    /**
     * 文档管理
     *
     * @return
     */
    @GetMapping("/searchMyDoc")
//    我的文档
    public ModelAndView searchMyDoc(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("searchMyDoc");
        User user =(User) session.getAttribute("loginUser");
        User user1 = userService.queryById(user.getId());
        modelAndView.addObject("user", user1);
        return modelAndView;
    }

    /**
     * 日程管理
     *
     * @return
     */
    @GetMapping("/searchNewsInfo")
//    员工日程查询
    public ModelAndView searchNewsInfo() {
        ModelAndView modelAndView = new ModelAndView("searchNewsInfo");
         List<News>  news=newsService.queryAll();
         modelAndView.addObject("news",news);
        return modelAndView;
    }

}
